package com.ljkj.blockpay.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.ljkj.blockpay.basedata.BasePresenterImpl;
import com.ljkj.blockpay.model.UserModel;
import com.ljkj.blockpay.network.MainRepository;
import com.ljkj.blockpay.network.response.DisposableCallBack;
import com.ljkj.blockpay.presenter.inter.LoginInter;
import com.ljkj.blockpay.util.Preferences;
import com.ljkj.blockpay.util.ToastUtils;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import rx.schedulers.Schedulers;

/**
 * Created by yp on 2022/3/4
 */
public class PhoneLoginPresenter extends BasePresenterImpl<LoginInter.LoginInterView> implements LoginInter.LoginInterPresenter {

    public PhoneLoginPresenter(@NonNull Context context, @NonNull LoginInter.LoginInterView view) {
        super(context, view);
    }
    @Override
    public void userLogin(Context context ,String code, String countryCode, String email, String mobile, String password) {
                addDisposable(MainRepository.getInstance().userLogin(code, countryCode, email, mobile, password)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableCallBack<UserModel>(){
            @Override
            protected void onSafeSuccess(UserModel userModel) {
                super.onSafeSuccess(userModel);
                Preferences.getInstance().commit(getContext(),"login","token",userModel.getToken());
                if (userModel != null){
                    getView().loginSuccess();
                }
            }

                    @Override
                    protected void onFailed(String code, String message) {
                        super.onFailed(code, message);
                        com.luck.picture.lib.tools.ToastUtils.s(getContext(),message);
                    }
                }));
    }
}
